package controllers

import (
	"github.com/revel/revel"
	"strconv"
	"wuhuaguo.com/whg01/app/models/carts"
)

type Cart struct {
	WxApp
}

type PutRes struct {
	Code      int
	Msg       string
	NuminCart int
}

func (c Cart) Putintocart(itemid string) revel.Result {
	//获取用户
	user := c.wxId()
	id, _ := strconv.Atoi(itemid)
	carts.AddItemtoCart(user.UserId, id)
	sum := carts.GetUserItemNum(user.UserId)
	return c.RenderJson(PutRes{Code: 0, Msg: "商品已经放入购物车中", NuminCart: sum})
}

func (c Cart) Index() revel.Result {
	//获取用户购物车数据
	user := c.wxId()
	items := carts.GetUserItems(user.UserId)
	cost := float64(0.0)
	for i := 0; i < len(items); i++ {
		num := items[i].Num
		price := items[i].GetImtem().Price
		cost += float64(num) * price
	}
	return c.Render(user, items, cost)
}
